package com.liunian.algorithmstudy.listnode;

public class RemoveNthFromEnd19 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dumyNode = new ListNode(-1, head);
        ListNode pre = dumyNode;
        ListNode tail = head;
        for (int i = 0; i < n; i++) {
            tail = tail.next;
        }
        while (tail != null) {
            tail = tail.next;
            pre = pre.next;
        }
        pre.next = pre.next.next;
        return dumyNode.next;
    }

}
